<template>
  <div>
    <el-upload
      class="upload-demo"
      ref="upload"
      :headers="setHeaders"
      :action="baseURL + 'tbNetDisk/upload'"
      :before-upload="beforeUpload"
      :on-preview="handlePreview"
      :on-change="handleChange"
      :on-progress="handleprogress"
      :on-success="handleSuccess"
      :on-error="handleError"
      :on-remove="handleRemove"
      :before-remove="beforeRemove"
      :auto-upload="false"
      :multiple="multiple"
      :accept="accept"
      :data="initData"
    >
      <el-button size="small" :disabled="disabled" type="primary" plain>
        {{ buttonTxt }}
      </el-button>
    </el-upload>
    <p v-show="accept" class="txt-explain">请上传{{ accept }}类型的文件</p>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { showTypeFilter } from '../common/fun'
export default {
  data() {
    return {
      setHeaders: { Authorization: sessionStorage.getItem('authorization') },
      fileList: [],
      fileData: [],
      accept: '',
      fileCount: 0,
      UpLoadFilesLength: 0,
      showTypeFilter,
    }
  },
  props: {
    initData: {
      type: Object,
    },
    buttonTxt: {
      type: String,
      default: '选择文件',
    },
    multiple: {
      //文件多选
      type: Boolean,
      default: false,
    },
    fileType: {
      //文件类型 0:全部  1：图片  2：视频  3：文本
      type: Number,
      default: 0,
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    defaultValue: {
      // type: Array,
      default: () => [],
    },
  },
  computed: {
    ...mapGetters(['netPid']),
  },
  mounted() {
    this.$bus.$on('uploading', () => {
      this.initData.pId = this.netPid
      if (this.fileCount == 0) {
        this.warnMsg('请选择文件')
        return
      }
      this.$refs.upload.submit()
    })
  },
  beforeDestroy() {
    this.$bus.$off('uploading')
  },
  methods: {
    // 文件上传成功时的钩子
    handleSuccess(res, file, fileList) {
      if (res.code == 0) {
        let length = this.$refs.upload.uploadFiles.length
        this.UpLoadFilesLength++
        if (length == this.UpLoadFilesLength) {
          this.$emit('emit')
          this.$refs.upload.clearFiles()
          this.$bus.$emit('refreshTbNetDiskList')
        }
      } else {
        this.$message({
          message: res.msg,
          type: 'warning',
        })
      }
    },
    handleChange(file, fileList) {
      this.fileCount = fileList.length
      if (this.multiple) return
      this.fileList = fileList.slice(-1)
    },
    // 上传文件之前的钩子
    beforeUpload(file) {
      let str = file.name.split('.').pop().toLowerCase()
      this.initData.showType = this.showTypeFilter(str)
      this.initData.pId = this.initData.pId || 0
      let maxSize = 200 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message({
          message: '您上传的资源过大，会影响加载速度，建议控制在200M以内',
          type: 'warning',
        })
      }
    },
    // 文件上传时的钩子
    handleprogress(event, file, fileList) {},
    // 点击文件列表中已上传的文件时的钩子
    handlePreview(file) {},
    beforeRemove(file, fileList) {
      // return this.$confirm(`确定移除 ${file.name}？`);
    },
    // 文件列表移除文件时的钩子
    handleRemove(file, fileList) {
      this.fileCount = fileList.length
      this.fileData = []
      if (fileList.length) {
        fileList.forEach((v) => {
          //判断是回显数据还是新上传数据
          if (v.hasOwnProperty('response')) {
            this.fileData.push(v.response.data[0])
          } else {
            this.fileData.push(v)
          }
        })
      }
      this.$emit('e-fileData', this.fileData)
    },
    handleError() {
      this.$message.error('上传失败，请重试')
    },
  },
}
</script>
<style scoped lang="scss">
</style>